﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Yunchang.ILUI
{
    /// <summary>
    /// UI基本逻辑控制单位
    /// </summary>
    public class UIController
    {
        public string signId { get; protected set; }
        public UIMono uiMono { get; protected set; }
        public bool hasMono { get; private set; }
        public RectTransform tranform { get { return hasMono ? uiMono.rectTransform : null; } }
        public GameObject gameObject { get { return hasMono ? uiMono.gameObject : null; } }
        public List<UIOrder> orderList { get { return hasMono ? uiMono.orderList : null; } }
        public bool activeSelf { get { return hasMono ? uiMono.gameObject.activeSelf : false; } }
        public bool activeInHierarchy { get { return hasMono ? uiMono.gameObject.activeInHierarchy : false; } }

        protected virtual void OnInitAsset(GameObject obj, RectTransform parent, bool positionRest)
        {
            hasMono = true;
            uiMono = obj.GetComponent<UIMono>();
            if (uiMono == null)
            {
                GLog.Error("This Page is not find UIMono");
            }
            if (parent != null && positionRest)
            {
                uiMono.transform.SetParent(parent);
                uiMono.transform.localPosition = Vector3.zero;
                uiMono.transform.localScale = Vector3.one;
                uiMono.transform.localRotation = Quaternion.Euler(Vector3.zero);
            }
            
            uiMono.OnInit(this, signId);
        }
        public virtual void SetActive(bool visible)
        {
            if (hasMono)
            {
                uiMono.gameObject.SetActive(visible);
            }
        }

        public virtual void Awake() { }

        public virtual void Start() { }
        public virtual void Update() { }
        public virtual void OnTransformParentChanged() { }

        public virtual void OnEnable() { }

        public virtual void OnDestroy()
        {
            if (hasMono)
            {
                uiMono = null;
                hasMono = false;
            }
        }
        public virtual void OnDisable() { }
    }
}